package com.ruoyi.ldzlsb.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ldzlsb.domain.SbInspectionDateDetail;
import com.ruoyi.ldzlsb.domain.SbInspectionItem;
import com.ruoyi.ldzlsb.mapper.SbInspectionDateDetailMapper;
import com.ruoyi.ldzlsb.service.ISbInspectionDateDetailService;

/**
 * 巡检日期明细Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SbInspectionDateDetailServiceImpl implements ISbInspectionDateDetailService 
{
    @Autowired
    private SbInspectionDateDetailMapper sbInspectionDateDetailMapper;

    /**
     * 查询巡检日期明细
     * 
     * @param detailId 巡检日期明细主键
     * @return 巡检日期明细
     */
    @Override
    public SbInspectionDateDetail selectSbInspectionDateDetailByDetailId(Long detailId)
    {
        return sbInspectionDateDetailMapper.selectSbInspectionDateDetailByDetailId(detailId);
    }

    /**
     * 查询巡检日期明细列表
     * 
     * @param sbInspectionDateDetail 巡检日期明细
     * @return 巡检日期明细
     */
    @Override
    public List<SbInspectionDateDetail> selectSbInspectionDateDetailList(SbInspectionDateDetail sbInspectionDateDetail)
    {
        return sbInspectionDateDetailMapper.selectSbInspectionDateDetailList(sbInspectionDateDetail);
    }

    /**
     * 新增巡检日期明细
     * 
     * @param sbInspectionDateDetail 巡检日期明细
     * @return 结果
     */
    @Override
    public int insertSbInspectionDateDetail(SbInspectionDateDetail sbInspectionDateDetail)
    {
        return sbInspectionDateDetailMapper.insertSbInspectionDateDetail(sbInspectionDateDetail);
    }

    /**
     * 修改巡检日期明细
     * 
     * @param sbInspectionDateDetail 巡检日期明细
     * @return 结果
     */
    @Override
    public int updateSbInspectionDateDetail(SbInspectionDateDetail sbInspectionDateDetail)
    {
        return sbInspectionDateDetailMapper.updateSbInspectionDateDetail(sbInspectionDateDetail);
    }

    /**
     * 批量删除巡检日期明细
     * 
     * @param detailIds 需要删除的巡检日期明细主键
     * @return 结果
     */
    @Override
    public int deleteSbInspectionDateDetailByDetailIds(Long[] detailIds)
    {
        return sbInspectionDateDetailMapper.deleteSbInspectionDateDetailByDetailIds(detailIds);
    }

    /**
     * 删除巡检日期明细信息
     * 
     * @param detailId 巡检日期明细主键
     * @return 结果
     */
    @Override
    public int deleteSbInspectionDateDetailByDetailId(Long detailId)
    {
        return sbInspectionDateDetailMapper.deleteSbInspectionDateDetailByDetailId(detailId);
    }
    
    /**
     * 根据日期记录ID查询巡检日期明细列表
     * 
     * @param dateRecordId 日期记录ID
     * @return 巡检日期明细集合
     */
    @Override
    public List<SbInspectionDateDetail> selectSbInspectionDateDetailByDateRecordId(Long dateRecordId)
    {
        return sbInspectionDateDetailMapper.selectSbInspectionDateDetailByDateRecordId(dateRecordId);
    }
    
    /**
     * 批量插入巡检日期明细
     * 
     * @param detailList 巡检日期明细列表
     * @return 结果
     */
    @Override
    public int batchInsertSbInspectionDateDetail(List<SbInspectionDateDetail> detailList)
    {
        if (detailList == null || detailList.isEmpty()) {
            return 0;
        }
        
        return sbInspectionDateDetailMapper.batchInsertSbInspectionDateDetail(detailList);
    }
    
    /**
     * 根据日期记录ID删除巡检日期明细
     * 
     * @param dateRecordId 日期记录ID
     * @return 结果
     */
    @Override
    public int deleteSbInspectionDateDetailByDateRecordId(Long dateRecordId)
    {
        return sbInspectionDateDetailMapper.deleteSbInspectionDateDetailByDateRecordId(dateRecordId);
    }
    
    /**
     * 根据巡检项列表生成巡检日期明细列表
     * 
     * @param dateRecordId 日期记录ID
     * @param items 巡检项列表
     * @return 巡检日期明细列表
     */
    @Override
    public List<SbInspectionDateDetail> generateDetailsFromItems(Long dateRecordId, List<SbInspectionItem> items)
    {
        List<SbInspectionDateDetail> details = new ArrayList<>();
        
        if (dateRecordId == null || items == null || items.isEmpty()) {
            return details;
        }
        
        for (SbInspectionItem item : items) {
            SbInspectionDateDetail detail = new SbInspectionDateDetail();
            detail.setDateRecordId(dateRecordId);
            detail.setItemId(item.getItemId());
            detail.setItemName(item.getItemName());
            detail.setStandardValue(item.getItemStandard());
            details.add(detail);
        }
        
        return details;
    }
    
    /**
     * 批量保存巡检日期明细
     * 
     * @param dateRecordId 日期记录ID
     * @param detailList 巡检日期明细列表
     * @return 结果
     */
    @Override
    @Transactional
    public int batchSaveDetails(Long dateRecordId, List<SbInspectionDateDetail> detailList)
    {
        if (dateRecordId == null || detailList == null || detailList.isEmpty()) {
            return 0;
        }
        
        // 先删除现有明细
        deleteSbInspectionDateDetailByDateRecordId(dateRecordId);
        
        // 设置日期记录ID
        for (SbInspectionDateDetail detail : detailList) {
            detail.setDateRecordId(dateRecordId);
        }
        
        // 批量插入
        return sbInspectionDateDetailMapper.batchInsertSbInspectionDateDetail(detailList);
    }
} 